package cn.jsc.user.controller;

import cn.jsc.basic.exception.BusinessException;
import cn.jsc.basic.util.AjaxResult;
import cn.jsc.user.service.IVerifycodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
/**
 * 短信验证码接口类
 */
@RestController//@ResponseBody+@Controller
@RequestMapping("/verifycode")
public class VerifyCodeController {
    @Autowired
    private IVerifycodeService verifycodeService;
    /**
     * 发送短信验证码接口
     * @param map
     * @return
     */
    @PostMapping("/smsCode")//Map在一定程度上可以当成对象使用 - 数据必须通过get方法获取
    public AjaxResult sendSmsCode(@RequestBody Map<String,String> map) {
        try {
            String phone = map.get("phone");
            verifycodeService.smsCode(phone);
            return new AjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("短信发送失败，" + e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统错误，" + e.getMessage());
        }
    }
    /**
     * 微信绑定：发送短信验证码接口
     * @param map
     * @return
     */
    @PostMapping("/binderCode")//Map在一定程度上可以当成对象使用 - 数据必须通过get方法获取
    public AjaxResult binderCode(@RequestBody Map<String,String> map) {
        try {
            String phone = map.get("phone");
            verifycodeService.binderCode(phone);
            return new AjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("短信发送失败，" + e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统错误，" + e.getMessage());
        }
    }

}
